﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace LingDong.DataProvider
{
    public class DataReaderFactory
    {
        /// <summary>
        /// Create a DataReader class
        /// Class name is read in config
        /// </summary>
        /// <returns></returns>
        public static IDataReader CreateDataReader()
        {
            string className = ConfigProvider.ConfigDataProvider.Default.DataReaderInstanceClassName;
            return CreateDataReader(className);
        }

        /// <summary>
        /// Create a DataReader class by class name
        /// </summary>
        /// <param name="className">the DataReader class name, such as SogouTDataReader</param>
        /// <returns></returns>
        public static IDataReader CreateDataReader(string className)
        {
            if (String.IsNullOrEmpty(className))
                return null;

            string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
            if (!className.StartsWith(assemblyName))
                className = assemblyName + "." + className;
            return (IDataReader)Assembly.GetExecutingAssembly().CreateInstance(className);
        }
    }
}
